package com.ruoyi.thread;

import com.ruoyi.common.config.FileStorageProperties;
import com.ruoyi.common.exception.FileErrorException;
import com.ruoyi.domain.Templates;
import com.ruoyi.mapper.TemplatesMapper;
import com.ruoyi.utils.FileToPNG;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.nio.file.Path;

/**
 * @Classname thread
 * @Description TODO
 * @Date 2025/3/21 14:40
 * @Created by xxx
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class thread implements Runnable {
    private Integer id;
    private Path savePath;
    private Path imageDir;
    private FileStorageProperties fileStorageProperties;
    private TemplatesMapper templatesMapper;

    @Override
    public void run() {
        int count = 0;
        try {
            count = FileToPNG.convertToImages(savePath.toString(), imageDir.toString());
        } catch (Exception e) {
            throw new FileErrorException("转换图片失败");
        }
        if (!savePath.endsWith(".pdf")) {

        }
        Path imagePath = imageDir.resolve("page.png");
        String base64 = FileToPNG.imageToBase64(imagePath.toString());
        Templates templates = Templates.builder()
                .id(id)
                .imageCount(count)
                .imageData(base64)
                .imageDir(imagePath.toString())
                .build();
        templatesMapper.updateById(templates);
    }
}
